package com.riko.comic.module.sys.controller;


import com.riko.comic.module.comic.bean.vo.ComicsVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.riko.comic.core.utils.MinioUtil;
import com.riko.comic.module.sys.bean.po.User;
import com.riko.comic.module.sys.bean.po.UserMenu;
import com.riko.comic.module.sys.bean.vo.UserVo;
import com.riko.comic.module.sys.service.ICollectService;
import com.riko.comic.module.sys.service.IUserMenuService;
import com.riko.comic.module.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Sun
 * @since 2021-11-01
 */
@Controller
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserMenuService userMenuService;
    @Autowired
    private ICollectService collectService;
    /**
     *
     * <p>
     * 个人主页
     * </p>
     * @author Sun
     * @since 2021-11-02
     *
     */
    @RequestMapping("/person")
    public String personSpace() {
        return "redirect:/sys/user/person/home";
    }
    /**
     *
     * <p>
     * 个人主页功能
     * </p>
     * @author Sun
     * @since 2021-11-03
     *
     */
    @RequestMapping("/person/{fun}")
    public String personFun(@PathVariable String fun,HttpServletRequest request){
        HttpSession httpSession = request.getSession();
        Integer id = (Integer) httpSession.getAttribute("loginUserId");
        User user = userService.getById(id);
        List<UserMenu> userMenuList = userMenuService.list();
        httpSession.setAttribute("user",user);
        httpSession.setAttribute("jsp",fun);
        httpSession.setAttribute("userMenuList",userMenuList);

        List<ComicsVo> list = collectService.queryByUserId(id);
        request.setAttribute("comicList", list);
        return "person";
    }

    /**
     *
     * @author Sun
     * @since 2021-11-02
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/userLogin")
    public void userLogin(HttpServletResponse response,HttpServletRequest request, UserVo userVo) throws IOException {

        Boolean userState = userService.login(response,request,userVo);//登录状态

        response.getWriter().write(String.valueOf(userState));
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    //提交校验、录入
    @RequestMapping("/userRegister")
    public void userRegister(User user, HttpServletResponse response) throws IOException {
        String message = userService.registerCheck(user);
        response.getWriter().write(message);
    }

    //用户名校验
    @RequestMapping("/usernameCheck")
    public void usernameCheck(String username, HttpServletResponse response) throws IOException {
        String message = userService.usernameCheck(username);
        response.getWriter().write(message);
    }

    //密码校验
    @RequestMapping("/passwordCheck")
    public void passwordCheck(String password, HttpServletResponse response) throws IOException {
        String message = userService.passwordCheck(password);
        response.getWriter().write(message);
    }
    /**
     *
     * @author Sun
     * @since 2021/11/04
     */
    @RequestMapping("/updateUser")
    public String upDataUser(User user, HttpServletRequest request, MultipartFile picFile){
        Integer id = (Integer) request.getSession().getAttribute("loginUserId");
        user.setId(id);
        try {
            //上传头像并获取url
            String url = MinioUtil.uploadFile(picFile);
            user.setPic(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        userService.updateById(user);
        return "redirect:/sys/user/person/home";
    }
    @RequestMapping("/recharge")
    public String rechargeCoin(Integer userId,Integer count){
        User user = userService.getById(userId);
        user.setCoin(user.getCoin()+count);
        userService.updateById(user);
        return "redirect:/sys/user/person/home";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("loginUserId");
        return "redirect:/comics/index";
    }
}
